package com.huhu.contact.view;

import com.huhu.contact.ResourceTable;
import ohos.agp.components.*;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.PopupDialog;
import ohos.app.Context;
import ohos.multimodalinput.event.MmiPoint;
import ohos.multimodalinput.event.TouchEvent;

public class SlideBar extends StackLayout implements Component.DrawTask, Component.TouchEventListener {


    private PopupDialog mPopupDialog;

    public interface OnTouchAssortListener {
        public void onTouchAssortListener(String s);
    }

    // 分类
    private static final String[] ASSORT_TEXT = {"A", "B", "C", "D", "E", "F", "G",
            "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
            "U", "V", "W", "X", "Y", "Z", "#"};

    private Paint mPaint = new Paint();
    private int mSelectIndex = -1;
    private OnTouchAssortListener mListener = null;
    Component layoutView;
    Text text;

    public SlideBar(Context context) {
        super(context);
        init(context);
    }

    public SlideBar(Context context, AttrSet attrs) {
        super(context, attrs);
        init(context);

    }

    public SlideBar(Context context, AttrSet attrs, String styleName) {
        super(context, attrs, styleName);
        init(context);
    }

    private void init(Context context) {
        setWidth(500);
        //布局初始化
        layoutView = LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_alert_dialog_menu, null, false);
        text = (Text) layoutView.findComponentById(ResourceTable.Id_content);
        ComponentContainer.LayoutConfig layoutConfig = layoutView.getLayoutConfig();
        layoutConfig.setMarginRight(AttrHelper.vp2px(40, mContext));
        layoutView.setLayoutConfig(layoutConfig);
        layoutView.setVisibility(INVISIBLE);
        addComponent(layoutView);
//        setEstimateSizeListener(this);
        if (!isClickable()) setClickable(true);
        setTouchEventListener(this);
        addDrawTask(this);
        mListener = null;
    }

    public void setOnTouchAssortListener(OnTouchAssortListener listener) {
        this.mListener = listener;
    }

//    public int width = 240;
//
//    public int height = 440;
//
//    @Override
//    public boolean onEstimateSize(int widthEstimateConfig, int heightEstimateConfig) {
//        int widthSpce = EstimateSpec.getMode(widthEstimateConfig);
//        int heightSpce = EstimateSpec.getMode(heightEstimateConfig);
//
//        int widthConfig = 0;
//        switch (widthSpce) {
//            case EstimateSpec.UNCONSTRAINT:
//            case EstimateSpec.PRECISE:
//                width = EstimateSpec.getSize(widthEstimateConfig);
//                widthConfig = EstimateSpec.getSizeWithMode(width, EstimateSpec.PRECISE);
//                break;
//            case EstimateSpec.NOT_EXCEED:
//                widthConfig = EstimateSpec.getSizeWithMode(width, EstimateSpec.PRECISE);
//                break;
//            default:
//                break;
//        }
//
//        int heightConfig = 0;
//        switch (heightSpce) {
//            case EstimateSpec.UNCONSTRAINT:
//            case EstimateSpec.PRECISE:
//                height = EstimateSpec.getSize(heightEstimateConfig);
//                heightConfig = EstimateSpec.getSizeWithMode(height, EstimateSpec.PRECISE);
//                break;
//            case EstimateSpec.NOT_EXCEED:
//                heightConfig = EstimateSpec.getSizeWithMode(height, EstimateSpec.PRECISE);
//                break;
//            default:
//                break;
//        }
//        System.out.println("WYT_width:" + width + "   height:" + height + "     width_spec:" + widthSpce + "     height_spec:" + heightSpce);
//        setEstimatedSize(widthConfig, heightConfig);
//        return true;
//    }


    @Override
    public void onDraw(Component component, Canvas canvas) {
        int nHeight = getHeight();
        int hWidth = getWidth();
        int nAssortCount = ASSORT_TEXT.length;
        int nInterval = nHeight / nAssortCount;

        for (int i = 0; i < nAssortCount; i++) {
            mPaint.setAntiAlias(true); // 抗锯齿
            mPaint.setFakeBoldText(true); // 默认粗体
            mPaint.setColor(new Color(0xff5f5f5f)); // 白色
            if (i == mSelectIndex) {
                // 被选择的字母改变颜色和粗体
                mPaint.setColor(new Color(0xff3399ff));
                mPaint.setFakeBoldText(true);
                mPaint.setTextSize(AttrHelper.fp2px(20, mContext));
            }
            float xPos = (hWidth * 0.75f) - mPaint.measureText(ASSORT_TEXT[i]) / 2; // 计算字母的X坐标
            float yPos = nInterval * i + nInterval; // 计算字母的Y坐标
            canvas.drawText(mPaint, ASSORT_TEXT[i], xPos, yPos);
            mPaint.reset();
        }
    }


    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        //判断是哪一个字母被点击了
        MmiPoint absPoint = touchEvent.getPointerPosition(touchEvent.getIndex());
        int nIndex = (int) (absPoint.getY() / getHeight() * ASSORT_TEXT.length);
        if (nIndex >= 0 && nIndex < ASSORT_TEXT.length) {
            switch (touchEvent.getAction()) {
                case TouchEvent.POINT_MOVE:
                    // 如果滑动改变
                    if (mSelectIndex != nIndex) {
                        mSelectIndex = nIndex;
                        showCharacter(ASSORT_TEXT[mSelectIndex]);
                        if (mListener != null) {
                            mListener.onTouchAssortListener(ASSORT_TEXT[mSelectIndex]);
                        }
                    }
                    break;
                case TouchEvent.PRIMARY_POINT_DOWN:
                    mSelectIndex = nIndex;
                    showCharacter(ASSORT_TEXT[mSelectIndex]);
                    if (mListener != null) {
                        mListener.onTouchAssortListener(ASSORT_TEXT[mSelectIndex]);
                    }

                    break;
                case TouchEvent.PRIMARY_POINT_UP:
                    disShowCharacter();
                    mSelectIndex = -1;
                    break;
            }
        } else {
            mSelectIndex = -1;
            disShowCharacter();
        }
        invalidate();
        return true;
    }


    private void disShowCharacter() {
        if (layoutView != null) {
            layoutView.setVisibility(INVISIBLE);
        }
    }

    /**
     * 显示弹出的字符
     *
     * @param string
     */
    private void showCharacter(String string) {
        if (layoutView != null && text != null) {
            text.setText(string);
            layoutView.setVisibility(VISIBLE);
        }
    }
} 

